﻿#pragma once

#include "component.h"
#include <QObject>


#define component (ComponentManager::GetInstance())

class SystemJournal;
class ComponentManager : public QObject
{
	Q_OBJECT

public:
	ComponentManager(QObject *parent);
	~ComponentManager();
	static ComponentManager* GetInstance();
	bool Init(const QVector<ComponentType>& coms = {});
	bool CheckComponent();
	bool ResetComponent();

/****************************************************************************
 * 功能: 获取组件
 * 传参: id: 组件类型， loaded: true 在列表中未找到组件时会自动重新加载组件， flase只从列表中获取
 * 返回值: 组件指针，获取失败为nullptr
 ****************************************************************************/
	Component* GetComponent(ComponentType id, bool load = false);
	Component* GetComponentByStr(const QString& str);
	Component* CreateNewComponent(ComponentType id);

	QString GetErrorMsg();

	void RegisterSystemEventCallBake(std::function<void(int)> callbake);
public slots:
	void DealWithComponentSystemEvent(int componet, int Event);
	void DealWithComponentWarningEvent(int componet, const QString& msg);
	void DealWithSystemEvent(int state);    // 系统状态处理
signals:
	void PublishSystemEvent(int Event); // 系统事件信号， 组件可以选择订阅

private:
	bool CreateAllComponet();
private:
	QString m_errmsg;
	SystemJournal* m_writer = nullptr;
	std::map<ComponentType, Component*> m_components;   // 插件组件, 未加载到系统

	QList<std::function<void(int)>> m_sytemCall;
};
